//
//  LzwAttentionVC.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/8.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwAttentionVC.h"
#import "LzwSingleton.h"
#import "LzwModleSmall.h"
#import "LzwAddViewController.h"
#import "LzwAttwntDetailVc.h"
@interface LzwAttentionVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *myAttentionTableView;

@end

@implementation LzwAttentionVC

-(void)dealloc{
    [_myAttentionTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的关注";
    
    UIImage *image = [UIImage imageNamed:@"return.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(LeftButtonAction:)];
    
    

    
    self.myAttentionTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myAttentionTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myAttentionTableView];
    
    [self.myAttentionTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.myAttentionTableView.delegate = self;
    self.myAttentionTableView.dataSource = self;
    
    [_myAttentionTableView release];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [[LzwSingleton shareDataBase]selectAttentionTable].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableArray *array = [[LzwSingleton shareDataBase] selectAttentionTable];
    
    LzwModleSmall *modle  = [array objectAtIndex:indexPath.row];
    
    cell.textLabel.text = modle.content;
    cell.textLabel.textColor = [UIColor lightGrayColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}
//删除cell上面的数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //先从数据库中把数据取出来 删掉, 然后在删除cell上面的数据
    
    NSMutableArray *array = [[LzwSingleton shareDataBase]selectAttentionTable];
    
    LzwModleSmall *modle = [array objectAtIndex:indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [[LzwSingleton shareDataBase] deleteAttentionTable:modle.content];
        
        [array removeObjectAtIndex:indexPath.row];
        
        
        //删除cell
        
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
        
        
    }

    
    
}

//点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwAttwntDetailVc *attenVc = [[LzwAttwntDetailVc alloc]init];
    
    NSMutableArray *array = [[LzwSingleton shareDataBase] selectAttentionTable];
    
    LzwModleSmall *modle  = [array objectAtIndex:indexPath.row];
    attenVc.string = modle.share_url;
    
    [self.navigationController pushViewController:attenVc animated:YES];
    
    
}

//返回按钮
-(void)LeftButtonAction:(UIBarButtonItem *)but{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
